﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Data.Common;
using Data.Enum;
using Data.Model;
using Data.ViewModel;

namespace DataAccess.SystemManage
{
    /// <summary>
    ///     用户管理，使用该系统的用户管理
    /// </summary>
    public class SysUser : DbBase, ISysUser
    {
        /// <summary>
        ///     通过id获取指定用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultInfo<jsh_user> GetUser(int id)
        {
            var result = new ResultInfo<jsh_user> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                var u = new jsh_user();
                if (id > 0)
                {
                    sql = string.Format("select * from jsh_user where id={0}", id);
                    DataSet ds = ExcuteSqlDataSet(sql);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        result.ResultNumber = ResultEnum.Success;
                        u = new jsh_user(ds.Tables[0].Rows[0]);
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        result.Describe = "不存在该用户";
                    }
                    result.Obj = u;
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "查询用户失败";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }

            return result;
        }

        /// <summary>
        ///     通过用户名获取用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ResultInfo<jsh_user> GetUser(string userName)
        {
            var result = new ResultInfo<jsh_user> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                var u = new jsh_user();
                if (!string.IsNullOrEmpty(userName))
                {
                    sql = string.Format("select * from jsh_user where username='{0}'", userName);
                    DataSet ds = ExcuteSqlDataSet(sql);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        result.ResultNumber = ResultEnum.Success;
                        u = new jsh_user(ds.Tables[0].Rows[0]);
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        result.Describe = "不存在该用户";
                    }
                    result.Obj = u;
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "查询用户失败";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }

            return result;
        }

        /// <summary>
        ///     通过用户模糊获取用户列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ResultInfo<PagingDataSet<UserView>> GetUsers(int pageIndex, int pageSize, string userName)
        {
            var result = new ResultInfo<PagingDataSet<UserView>>();
            try
            {
                using (SqlCommand myCommand = GetProCommand())
                {
                    myCommand.CommandText = "Common_GetPagingRecords";
                    var fromClause = new StringBuilder();
                    fromClause.AppendFormat("jsh_user R with (nolock)");

                    var whereClause = new StringBuilder();
                    if (!string.IsNullOrEmpty(userName))
                    {
                        whereClause.Append(string.Format(" and R.username like '%{0}%' ", userName));
                    }
                    if (whereClause.Length > 3)
                    {
                        whereClause.Remove(0, 5);
                    }

                    myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex - 1;
                    myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                    myCommand.Parameters.Add("@FromClause", SqlDbType.VarChar, 1024).Value = fromClause.ToString();
                    myCommand.Parameters.Add("@SelectFields", SqlDbType.VarChar, 512).Value = "R.*";
                    myCommand.Parameters.Add("@WhereClause", SqlDbType.NVarChar, 4000).Value = whereClause.ToString();
                    myCommand.Parameters.Add("@SortField", SqlDbType.VarChar, 256).Value = "R.id";
                    myCommand.Parameters.Add("@SortFieldIsDesc", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@ReturnRecordCount", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@MaxRecords", SqlDbType.Int).Value = int.MaxValue;
                    myCommand.Parameters.Add("@TotalRecords", SqlDbType.Int).Direction = ParameterDirection.Output;
                    myCommand.Parameters.Add("@ReverseOrder", SqlDbType.Bit).Direction = ParameterDirection.Output;

                    var list = new List<jsh_user>();
                    var dataSet = new DataSet();
                    var da = new SqlDataAdapter(myCommand);
                    da.Fill(dataSet);

                    if (dataSet != null)
                    {
                        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                        {
                            var jshAccount = new jsh_user(dataRow);
                            if (jshAccount.TranByConstructFlag)
                            {
                                list.Add(jshAccount);
                            }
                        }
                    }

                    int totalRecords = Convert.ToInt32(myCommand.Parameters["@TotalRecords"].Value);
                    bool reverseOrder = Convert.ToBoolean(myCommand.Parameters["@ReverseOrder"].Value);

                    if (reverseOrder)
                        list.Reverse();

                    var listView = PopulateJshUser(list);

                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = new PagingDataSet<UserView>
                    {
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        Records = listView,
                        TotalRecords = totalRecords
                    };
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        /// <summary>
        /// 混合查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userName">用户名</param>
        /// <param name="roleId">角色id</param>
        /// <param name="priId">权限id</param>
        /// <param name="resName">资源id</param>
        /// <returns></returns>
        public ResultInfo<PagingDataSet<UserView>> GetMulUsers(int pageIndex,int pageSize,string userName,int roleId,int priId,string resName)
        {
            ResultInfo<PagingDataSet<UserView>> result = new ResultInfo<PagingDataSet<UserView>>();
            try
            {
                using (SqlCommand myCommand = GetProCommand())
                {
                    myCommand.CommandText = "Common_GetPagingRecords";
                    var fromClause = new StringBuilder();
                    fromClause.AppendFormat("jsh_user R with (nolock)");

                    var whereClause = new StringBuilder();
                    if (!string.IsNullOrEmpty(userName))
                    {
                        whereClause.Append(string.Format(" and R.username like '%{0}%' ", userName));
                    }
                    if(roleId>0)
                    {
                        whereClause.Append(string.Format(" and R.id in (select userid from userrole where roleid={0} )",
                                                         roleId));
                    }
                    if(priId>0)
                    {
                        whereClause.Append(
                            string.Format(
                                " and R.id in (select userid from userrole where roleid in (select roleid from roleprivilege where privilegeid={0}))",
                                priId));
                    }
                    if(!string.IsNullOrEmpty(resName))
                    {
                        whereClause.Append(
                            string.Format(
                                " and R.id in (select userid from userrole where roleid in (select roleid from roleprivilege where privilegeid in (select privilegeid from privilege where privilegeresource like '%{0}%')))",
                                resName));
                    }
                    if (whereClause.Length > 3)
                    {
                        whereClause.Remove(0, 5);
                    }

                    myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex - 1;
                    myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                    myCommand.Parameters.Add("@FromClause", SqlDbType.VarChar, 1024).Value = fromClause.ToString();
                    myCommand.Parameters.Add("@SelectFields", SqlDbType.VarChar, 512).Value = "R.*";
                    myCommand.Parameters.Add("@WhereClause", SqlDbType.NVarChar, 4000).Value = whereClause.ToString();
                    myCommand.Parameters.Add("@SortField", SqlDbType.VarChar, 256).Value = "R.id";
                    myCommand.Parameters.Add("@SortFieldIsDesc", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@ReturnRecordCount", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@MaxRecords", SqlDbType.Int).Value = int.MaxValue;
                    myCommand.Parameters.Add("@TotalRecords", SqlDbType.Int).Direction = ParameterDirection.Output;
                    myCommand.Parameters.Add("@ReverseOrder", SqlDbType.Bit).Direction = ParameterDirection.Output;

                    var list = new List<jsh_user>();
                    var dataSet = new DataSet();
                    var da = new SqlDataAdapter(myCommand);
                    da.Fill(dataSet);

                    if (dataSet != null)
                    {
                        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                        {
                            var jshAccount = new jsh_user(dataRow);
                            if (jshAccount.TranByConstructFlag)
                            {
                                list.Add(jshAccount);
                            }
                        }
                    }

                    int totalRecords = Convert.ToInt32(myCommand.Parameters["@TotalRecords"].Value);
                    bool reverseOrder = Convert.ToBoolean(myCommand.Parameters["@ReverseOrder"].Value);

                    if (reverseOrder)
                        list.Reverse();

                    var listView = PopulateJshUser(list);

                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = new PagingDataSet<UserView>
                    {
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        Records = listView,
                        TotalRecords = totalRecords
                    };
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        private List<UserView> PopulateJshUser(IEnumerable<jsh_user> users)
        {
            int serial = 0;
            var userList = new List<UserView>();
            foreach (jsh_user user in users)
            {
                serial++;
                var roles = new StringBuilder();
                //TODO:点击查看用户角色
                //                var userRoles = _sysUser.GetUserRoles(1, int.MaxValue, user.id);
                //                for (int i = 0; i < userRoles.Obj.TotalRecords; i++)
                //                {
                //                    roles.Append(userRoles.Obj.Records[i].rolename);
                //                    roles.Append(",");
                //                }

                if (roles.Length <= 0)
                {
                    roles.Append(string.Empty);
                }
                userList.Add(new UserView
                {
                    Serial = serial,
                    Username = user.username,
                    Addr = user.addr,
                    Czqx = user.czqx,
                    Displayname = user.displayname,
                    Id=user.id,
                    Mobile = user.mobile,
                    Nation = user.nation,
                    Photo = user.photo,
                    Sex=user.sex
                });
            }
            return userList;
        }


        /// <summary>
        ///     根据用户id获取用户具有的角色
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public ResultInfo<PagingDataSet<UserRole>> GetUserRoles(int pageIndex, int pageSize, int userid)
        {
            var result = new ResultInfo<PagingDataSet<UserRole>>();
            if (userid > 0)
            {
                try
                {
                    using (SqlCommand myCommand = GetProCommand())
                    {
                        myCommand.CommandText = "Common_GetPagingRecords";
                        var fromClause = new StringBuilder();
                        fromClause.AppendFormat("UserRole R with (nolock)");

                        var whereClause = new StringBuilder();
                        if (userid > 0)
                        {
                            whereClause.Append(string.Format(" and R.userid = {0} ", userid));
                        }
                        if (whereClause.Length > 3)
                        {
                            whereClause.Remove(0, 5);
                        }

                        myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex - 1;
                        myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                        myCommand.Parameters.Add("@FromClause", SqlDbType.VarChar, 1024).Value = fromClause.ToString();
                        myCommand.Parameters.Add("@SelectFields", SqlDbType.VarChar, 512).Value = "R.*";
                        myCommand.Parameters.Add("@WhereClause", SqlDbType.NVarChar, 4000).Value =
                            whereClause.ToString();
                        myCommand.Parameters.Add("@SortField", SqlDbType.VarChar, 256).Value = "R.id";
                        myCommand.Parameters.Add("@SortFieldIsDesc", SqlDbType.Bit).Value = true;
                        myCommand.Parameters.Add("@ReturnRecordCount", SqlDbType.Bit).Value = true;
                        myCommand.Parameters.Add("@MaxRecords", SqlDbType.Int).Value = int.MaxValue;
                        myCommand.Parameters.Add("@TotalRecords", SqlDbType.Int).Direction = ParameterDirection.Output;
                        myCommand.Parameters.Add("@ReverseOrder", SqlDbType.Bit).Direction = ParameterDirection.Output;

                        var list = new List<UserRole>();
                        var dataSet = new DataSet();
                        var da = new SqlDataAdapter(myCommand);
                        da.Fill(dataSet);

                        if (dataSet != null)
                        {
                            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                            {
                                var jshAccount = new UserRole(dataRow);
                                if (jshAccount.TranByConstructFlag)
                                {
                                    list.Add(jshAccount);
                                }
                            }
                        }

                        int totalRecords = Convert.ToInt32(myCommand.Parameters["@TotalRecords"].Value);
                        bool reverseOrder = Convert.ToBoolean(myCommand.Parameters["@ReverseOrder"].Value);

                        if (reverseOrder)
                            list.Reverse();

                        result.ResultNumber = ResultEnum.Success;
                        result.Obj = new PagingDataSet<UserRole>
                        {
                            PageIndex = pageIndex,
                            PageSize = pageSize,
                            Records = list,
                            TotalRecords = totalRecords
                        };
                    }
                }
                catch (Exception e)
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = e.Message;
                }
            }
            else
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = "选取的用户无效";
            }
            return result;
        }

        /// <summary>
        ///     获取用户还没有的角色
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ResultInfo<PagingDataSet<Role>> GetUserUnRoles(int userid, int pageIndex, int pageSize)
        {
            var result = new ResultInfo<PagingDataSet<Role>>();
            try
            {
                using (SqlCommand myCommand = GetProCommand())
                {
                    myCommand.CommandText = "Common_GetPagingRecords";
                    var fromClause = new StringBuilder();
                    fromClause.AppendFormat("Role R with (nolock)");

                    var whereClause = new StringBuilder();
                    if (userid > 0)
                    {
                        whereClause.Append(
                            string.Format(
                                " and r.roleid not in (select ur.roleid from userrole ur where ur.userid={0}) ", userid));
                    }
                    if (whereClause.Length > 3)
                    {
                        whereClause.Remove(0, 5);
                    }

                    myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex - 1;
                    myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                    myCommand.Parameters.Add("@FromClause", SqlDbType.VarChar, 1024).Value = fromClause.ToString();
                    myCommand.Parameters.Add("@SelectFields", SqlDbType.VarChar, 512).Value = "R.*";
                    myCommand.Parameters.Add("@WhereClause", SqlDbType.NVarChar, 4000).Value = whereClause.ToString();
                    myCommand.Parameters.Add("@SortField", SqlDbType.VarChar, 256).Value = "R.RoleID";
                    myCommand.Parameters.Add("@SortFieldIsDesc", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@ReturnRecordCount", SqlDbType.Bit).Value = true;
                    myCommand.Parameters.Add("@MaxRecords", SqlDbType.Int).Value = int.MaxValue;
                    myCommand.Parameters.Add("@TotalRecords", SqlDbType.Int).Direction = ParameterDirection.Output;
                    myCommand.Parameters.Add("@ReverseOrder", SqlDbType.Bit).Direction = ParameterDirection.Output;

                    var list = new List<Role>();
                    var dataSet = new DataSet();
                    var da = new SqlDataAdapter(myCommand);
                    da.Fill(dataSet);

                    if (dataSet != null)
                    {
                        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                        {
                            var jshAccount = new Role(dataRow);
                            if (jshAccount.TranByConstructFlag)
                            {
                                list.Add(jshAccount);
                            }
                        }
                    }

                    int totalRecords = Convert.ToInt32(myCommand.Parameters["@TotalRecords"].Value);
                    bool reverseOrder = Convert.ToBoolean(myCommand.Parameters["@ReverseOrder"].Value);

                    if (reverseOrder)
                        list.Reverse();

                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = new PagingDataSet<Role>
                    {
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        Records = list,
                        TotalRecords = totalRecords
                    };
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        /// <summary>
        ///     创建或更新用户信息，如果用户id是大于0的，就是更新，如果用户id是小于等于0的，就是创建
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public ResultInfo<int> CreateUpdateUser(jsh_user user)
        {
            var result = new ResultInfo<int> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                if (user != null)
                {
                    if (user.id > 0)
                    {
                        sql =
                            string.Format(
                                "UPDATE jsh_user SET [username]='{0}',[displayname]='{1}',[password]='{2}',[sex]='{3}',[nation]='{4}',[zjlx]='{5}',[zjhm]='{6}',[type]='{7}',[czqx]='{8}',[telephone]='{9}',[mobile]='{10}',[addr]='{11}' where id={12} select {12} id",
                                user.username, user.displayname, user.password, user.sex, user.nation, user.zjlx,
                                user.zjhm, user.type, user.czqx, user.telephone, user.mobile, user.addr, user.id);
                    }
                    else
                    {
                        string check = string.Format("select id from jsh_user where username='{0}'", user.username);
                        DataSet dt = ExcuteSqlDataSet(check);
                        if (dt != null && dt.Tables[0].Rows.Count > 0)
                        {
                            result.ResultNumber = ResultEnum.Error;
                            result.Describe = "已经存在同名用户，请重新输入";
                        }
                        else
                        {
                            sql =
                                string.Format(
                                    "insert into jsh_user([username],[displayname],[password],[sex],[nation],[zjlx],[zjhm],[type],[czqx],[telephone],[mobile],[addr]) " +
                                    "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}') select @@identity id",
                                    user.username, user.displayname, user.password, user.sex, user.nation, user.zjlx,
                                    user.zjhm, user.type, user.czqx, user.telephone, user.mobile, user.addr, user.photo);
                        }
                    }
                    if (!string.IsNullOrEmpty(sql))
                    {
                        DataSet dataSet = ExcuteSqlDataSet(sql);
                        if (dataSet != null && dataSet.Tables[0].Rows.Count == 1)
                        {
                            int temp = Convert.ToInt32(dataSet.Tables[0].Rows[0]["id"]);
                            if (temp > 0)
                            {
                                result.ResultNumber = ResultEnum.Success;
                                result.Obj = temp;
                            }
                            else
                            {
                                result.ResultNumber = ResultEnum.Error;
                                result.Describe = "更新数据失败";
                            }
                        }
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "请输入有效数据";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        /// <summary>
        ///     删除指定id的用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultInfo<bool> DeleteUser(int id)
        {
            var result = new ResultInfo<bool> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Format("delete from jsh_user where id={0}", id);
                int temp = ExcuteSql(sql);
                if (temp > 0)
                {
                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = true;
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Obj = false;
                }
            }
            catch (Exception)
            {
            }
            return result;
        }

        /// <summary>
        ///     创建或更新用户角色信息
        /// </summary>
        /// <param name="ur"></param>
        /// <returns></returns>
        public ResultInfo<int> CreateUpdateUserRole(UserRole ur)
        {
            var result = new ResultInfo<int>();
            try
            {
                string sql = string.Empty;
                if (ur != null)
                {
                    if (ur.Id > 0)
                    {
                        sql =
                            string.Format(
                                "UPDATE UserRole SET [userid]={0},[RoleID]={1},[username]='{3}',[rolename]='{4}' where [ID]={2} select {2} id",
                                ur.userid, ur.Roleid, ur.Id, ur.username, ur.rolename);
                    }
                    else
                    {
                        string check = string.Format("select id from UserRole where [userid]={0} and [RoleID]={1}",
                            ur.userid, ur.Roleid);
                        DataSet dt = ExcuteSqlDataSet(check);
                        if (dt != null && dt.Tables[0].Rows.Count > 0)
                        {
                            result.ResultNumber = ResultEnum.Error;
                            result.Describe = "已经存在相同用户角色对应关系，请重新输入";
                        }
                        else
                        {
                            sql =
                                string.Format(
                                    "insert into UserRole([userid],[RoleID],[username],[rolename]) values({0},{1},'{2}','{3}') select @@identity id",
                                    ur.userid, ur.Roleid, ur.username, ur.rolename);
                        }
                    }
                    if (!string.IsNullOrEmpty(sql))
                    {
                        DataSet dataSet = ExcuteSqlDataSet(sql);
                        if (dataSet != null && dataSet.Tables[0].Rows.Count == 1)
                        {
                            int temp = Convert.ToInt32(dataSet.Tables[0].Rows[0]["id"]);
                            if (temp > 0)
                            {
                                result.ResultNumber = ResultEnum.Success;
                                result.Obj = temp;
                            }
                            else
                            {
                                result.ResultNumber = ResultEnum.Error;
                                result.Describe = "更新数据失败";
                            }
                        }
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "请输入有效数据";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        /// <summary>
        ///     删除指定用户角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultInfo<bool> DeleteUserRole(int id)
        {
            var result = new ResultInfo<bool> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Format("delete from userrole where id={0}", id);
                int temp = ExcuteSql(sql);
                if (temp > 0)
                {
                    result.ResultNumber = ResultEnum.Success;
                    result.Obj = true;
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Obj = false;
                }
            }
            catch (Exception)
            {
            }
            return result;
        }

        /// <summary>
        ///     修改用户密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public ResultInfo<int> ModifierUserPassword(string userName, string oldPassword, string newPassword)
        {
            var result = new ResultInfo<int> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                string check = string.Format("select id from jsh_user where username='{0}' and [Password]='{1}'",
                    userName, oldPassword);
                DataSet dt = ExcuteSqlDataSet(check);
                if (dt != null && dt.Tables[0].Rows.Count > 0)
                {
                    if (dt.Tables[0].Rows.Count == 1)
                    {
                        sql = string.Format("UPDATE jsh_user SET [Password]='{1}' where [UserName]='{0}'", userName,
                            newPassword);

                        if (!string.IsNullOrEmpty(sql))
                        {
                            DataSet dataSet = ExcuteSqlDataSet(sql);
                            if (dataSet != null && dataSet.Tables[0].Rows.Count == 1)
                            {
                                int temp = Convert.ToInt32(dataSet.Tables[0].Rows[0]["id"]);
                                if (temp > 0)
                                {
                                    result.ResultNumber = ResultEnum.Success;
                                    result.Obj = temp;
                                }
                                else
                                {
                                    result.ResultNumber = ResultEnum.Error;
                                    result.Describe = "更新数据失败";
                                }
                            }
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        result.Describe = "系统中存在同名切密码相同的用户，请联系管理员处理";
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "用户名或原始密码错误";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        /// <summary>
        ///     用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ResultInfo<jsh_user> Login(string userName, string password)
        {
            var result = new ResultInfo<jsh_user> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                var u = new jsh_user();
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "输入的用户名或密码为空";
                }
                else
                {
                    sql = string.Format("select * from jsh_user where username='{0}' and password='{1}'", userName,
                        password);
                    DataSet ds = ExcuteSqlDataSet(sql);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        result.ResultNumber = ResultEnum.Success;
                        u = new jsh_user(ds.Tables[0].Rows[0]);
                        if (u.TranByConstructFlag)
                        {
                            result.Obj = u;
                        }
                    }
                    else
                    {
                        result.ResultNumber = ResultEnum.Error;
                        result.Describe = "用户名或密码错误";
                    }
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }
    }
}